package SeparateWriter;

import com.phidgets.PhidgetException;
import com.phidgets.TemperatureSensorPhidget;

import java.io.IOException;
import java.text.DecimalFormat;

/**
 * User: Mike
 * Date: 12/11/11
 *
 * This class is the main class for the multicast server that broadcasts the
 *
 */
public class TempReadWrite {

    private static void temperature() {
        TemperatureSensorPhidget tempsensor;
        try {
            tempsensor = new TemperatureSensorPhidget();
            System.out.println("temperature started");
            // testing phase-----------------------------------------------------
            tempsensor.openAny();
            System.out.println("waiting for TemperatureSensor attachment...");
            tempsensor.waitForAttachment();
            tempsensor.setTemperatureChangeTrigger(0, 0.2);
            tempsensor.setThermocoupleType(0, TemperatureSensorPhidget.PHIDGET_TEMPERATURE_SENSOR_J_TYPE);
            System.out.println("captured TemperatureSensor attachment.");
            // testing phase-----------------------------------------------------

            while (Data.programRunning) {
                String filePath = "C:\\WhiskeyData\\outputTempWriter.txt";
                Writer.WriteData(GetTemps(tempsensor), filePath);
                try {
                    int secondsToSleep = 2;
                    Thread.sleep((long) secondsToSleep * 1000);            // sleep for X ms
                } catch (InterruptedException ignored) {
                }
            }
        } catch (PhidgetException e2) {
            e2.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String GetTemps(TemperatureSensorPhidget tempsensor) throws PhidgetException, IOException {

        final double calibration = 0.1;
        // testing phase-----------------------------------------------------
        double columnDC = tempsensor.getTemperature(0) - calibration;
        double ambientDC = tempsensor.getAmbientTemperature() - calibration;
//        double columnDC = 98.6 - calibration;
//        double ambientDC = 198.6 - calibration;
        // testing phase-----------------------------------------------------

        double columnDF = columnDC * 9.0 / 5.0 + 32.0;
        double ambientDF = ambientDC * 9.0 / 5.0 + 32.0;
        DecimalFormat df = new DecimalFormat("###.###");
        columnDC = Double.valueOf(df.format(columnDC));
        ambientDC = Double.valueOf(df.format(ambientDC));
        columnDF = Double.valueOf(df.format(columnDF));
        ambientDF = Double.valueOf(df.format(ambientDF));
        Long milliTime = System.currentTimeMillis();
        String lineToWrite = milliTime + "," + columnDC + "," + columnDF + "," + ambientDC + "," + ambientDF;
        System.out.println("Column = (" + columnDC + "," + columnDF+") Ambient = ("+ambientDC+","+ambientDF+")");
        return lineToWrite;
    }

    public static void main(String[] args) {
        temperature();
    }
}
